package com.example.madman.yuejian;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.example.madman.yuejian.Fragment.ActivityListFragment;
import com.example.madman.yuejian.Fragment.ClubListFragment;
import com.example.madman.yuejian.Fragment.CoachListFragment;
import com.example.madman.yuejian.Fragment.CourseListFragment;
import com.example.madman.yuejian.Fragment.MatchFragment;
import com.example.madman.yuejian.Fragment.PersonFragment;
import com.example.madman.yuejian.Fragment.ShowPicFragment;
import com.example.madman.yuejian.Fragment.TabFragment;

public class MainActivity extends FragmentActivity {

    private TextView tvclub,tvactivity,tvmatch,tvpic,tvperson;
    private ViewPager vp;
    private ClubListFragment fragment_club;
    private MatchFragment fragment_match;
    private CourseListFragment fragment_course;
    private ShowPicFragment fragment_pic;
    private PersonFragment fragment_person;
    private CoachListFragment fragment_coach;
    private ActivityListFragment fragment_activity;
    private FragmentPagerAdapter fpadapter;

    private TabFragment tabFragment;
    private int currentId = R.id.tv_club;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabFragment = new TabFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.main_container,tabFragment).commit();
        tvclub = (TextView) findViewById(R.id.tv_club);
        tvclub.setSelected(true);
        tvactivity = (TextView) findViewById(R.id.tv_activity);
        tvmatch = (TextView) findViewById(R.id.tv_match);
        tvpic = (TextView)findViewById(R.id.tv_pic);
        tvperson = (TextView) findViewById(R.id.tv_person);
       // vp = (ViewPager) findViewById(R.id.view_pager);

        initlistener();

    }



    private void initlistener(){
        tvclub.setOnClickListener(tabClickListener);
        tvactivity.setOnClickListener(tabClickListener);
        tvmatch.setOnClickListener(tabClickListener);
        tvpic.setOnClickListener(tabClickListener);
        tvperson.setOnClickListener(tabClickListener);
    }
    private View.OnClickListener tabClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() != currentId) {//如果当前选中跟上次选中的一样,不需要处理
               changeSelect(v.getId());//改变图标跟文字颜色的选中
                changeFragment(v.getId());//fragment的切换
                currentId = v.getId();//设置选中id
            }
        }
    };
    private void changeFragment(int resId) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();//开启一个Fragment事务

        hideFragments(transaction);
        if(resId==R.id.tv_pic){
            if(fragment_pic==null){
                fragment_pic = new ShowPicFragment();
                transaction.add(R.id.main_container,fragment_pic);
            }else {
                transaction.show(fragment_pic);
            }
        }else if(resId==R.id.tv_activity){
            if(fragment_activity==null){
                fragment_activity = new ActivityListFragment();
                transaction.add(R.id.main_container,fragment_activity);
            }else {
                transaction.show(fragment_activity);
            }
        }else if(resId == R.id.tv_match){
            if(fragment_match == null){
                fragment_match = new MatchFragment();
                transaction.add(R.id.main_container,fragment_match);
            }else {
                transaction.show(fragment_match);
            }
        }
        else if(resId == R.id.tv_club){
            if(tabFragment == null){
                tabFragment = new TabFragment();
                transaction.add(R.id.main_container,tabFragment);
            }else {
                transaction.show(tabFragment);
            }
        }
        else if(resId==R.id.tv_person){
            if(fragment_person==null){
                fragment_person = new PersonFragment();
                transaction.add(R.id.main_container,fragment_person);
            }else {
                transaction.show(fragment_person);
            }
        }

        transaction.commit();
    }
    private void hideFragments(FragmentTransaction transaction){
        if (tabFragment != null)
            transaction.hide(tabFragment);
        if (fragment_activity != null)
            transaction.hide(fragment_activity);
        if (fragment_pic != null)
            transaction.hide(fragment_pic);
        if (fragment_person != null)
            transaction.hide(fragment_person);
        if (fragment_coach != null)
            transaction.hide(fragment_match);
    }

    private void changeSelect(int resId) {
        tvclub.setSelected(false);
        tvactivity.setSelected(false);
        tvmatch.setSelected(false);
        tvpic.setSelected(false);
        tvperson.setSelected(false);

        switch (resId) {
            case R.id.tv_club:
                tvclub.setSelected(true);
                break;
            case R.id.tv_activity:
                tvactivity.setSelected(true);
                break;
            case R.id.tv_match:
                tvmatch.setSelected(true);
                break;
            case R.id.tv_pic:
                tvpic.setSelected(true);
                break;
            case R.id.tv_person:
                tvperson.setSelected(true);
                break;
        }
    }


}

